package com.lr.controller;

import com.deepoove.poi.data.PictureRenderData;
import com.lr.entity.Collect;
import com.lr.entity.JobHuntingLog;
import com.lr.entity.Recruit;
import com.lr.entity.Resume;
import com.lr.service.GRService;
import com.lr.util.VerifyCodeUtil;
import com.lr.util.WSDJS;
import com.lr.util.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class GRController {
    @Value("${VDirPath}")
    private String VDirPath;
    @Value("${VPath}")
    private String vPath;
    @Autowired
    private GRService grService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${WSD}")
    private int WSD;
    @Value("${ResumeName}")
    private String ResumeName;

    @RequestMapping("/grRegister")
    public Integer  Register(@RequestParam(value = "userName")String userName,@RequestParam(value = "password")String password,@RequestParam(value = "phone")String phone){
        return grService.Register(userName,password,phone);
    }
    //个人生成验证码
    @RequestMapping("/grCreateCode")
    public void grCreateCode(HttpServletResponse resp, HttpSession session) throws Exception {
        OutputStream out=resp.getOutputStream();
        String code= VerifyCodeUtil.generateVerifyCode(4);
        //session.setAttribute("grcode", code);  //存入session中
        //redisTemplate.opsForValue().set(session.getId()+"grcode",code,60, TimeUnit.SECONDS);  redis中
        redisTemplate.opsForValue().set(session.getId()+"grcode",code);  //redis中
        VerifyCodeUtil.outputImage(86, 30, out, code);
        out.close();
    }
    @RequestMapping(value = "/grLogin",method={RequestMethod.POST})
    public String  login(@RequestParam(value = "phone")String phone,@RequestParam(value = "password")String password,@RequestParam(value = "code")String code,HttpSession session){
        return grService.login(phone,password,code,session);
    }
    @RequestMapping(value = "/gr/grInfo.action",method={RequestMethod.GET})
    public Map grIndexInfo(ServletRequest request){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        return grService.logInfo(userJSON);
    }
    @RequestMapping(value = "/gr/grUpdateisPublic.action",method={RequestMethod.POST})
    public Integer upPublic(ServletRequest request,Integer ispublic){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");

        return grService.upPublic(token,ispublic);
    }
    @RequestMapping(value = "/gr/getResume.action",method={RequestMethod.POST})
    public Resume getResume(ServletRequest request){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        return grService.getResume(token);
    }
    @RequestMapping("/gr/uploadHeadImage.action")
    public String uploadHeadImage(MultipartFile file) throws IllegalAccessException, IOException {
        File f=new File(VDirPath);
        if(!f.exists()){
            f.mkdirs();
        }
        String headImage= UUID.randomUUID().toString()+".jpg";
        file.transferTo(new File(f,headImage));
        return vPath+File.separator+headImage;
    }
    @RequestMapping("/gr/updateResume.action")
    public Integer updateResume(Resume resume) throws IllegalAccessException {
        int jlwsd= WSDJS.jlwsd(resume);
        if(jlwsd<WSD){
            resume.setN_isPublic(0);
        }
        return grService.updateResume(resume);
    }
    @RequestMapping("/gr/grResumeDownload.action")
    public void grResumeDownload(ServletRequest request, ServletResponse resps) throws IllegalAccessException {
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        HttpServletResponse resp=(HttpServletResponse) resps;
        Resume resume=grService.getResume(token);
        Map<String,Object> map=new HashMap<String,Object>();
        String sex="";
        if(resume.getSex()==1) {
            sex="男";
        }
        if(resume.getSex()==2) {
            sex="女";
        }
        map.put("sex", sex);
        map.put("name", resume.getName());
        map.put("birthday", resume.getBirthday());
        map.put("natio", resume.getNatio());
        map.put("zzmm", resume.getZzmm());
        map.put("height", resume.getHeight());
        map.put("address", resume.getAddress());
        map.put("phone", resume.getPhone());
        map.put("email", resume.getEmail());
        map.put("whcd", resume.getWhcd());
        map.put("educateTime", resume.getEducateTime());
        map.put("school", resume.getSchool());
        map.put("major", resume.getMajor());
        map.put("course", resume.getCourse());
        map.put("jobExper", resume.getJobExper());
        map.put("schoolExper", resume.getSchoolExper());
        map.put("skill", resume.getSkill());
        map.put("self", resume.getSelf());
        map.put("gznx", resume.getGznx());
        //读取图片
        if(resume.getHeadImage()!=null && !"".equals(resume.getHeadImage())){
            map.put("headImage", new PictureRenderData(180, 180,VDirPath+File.separator+resume.getHeadImage().substring(vPath.length()) ));
        }
        WordUtil.createWordAndDownload(resp, map, VDirPath+ File.separator+ResumeName,resume.getName()+".docx");
    }
    @RequestMapping("/gr/searchRecruit.action")
    public List<Recruit> searchRecruit(Recruit recruit){
        return grService.searchRecruit(recruit);
    }
    @RequestMapping("/gr/addCollect.action")
    public Integer addCollect(ServletRequest request,String recruitIds){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        return grService.addCollect(userJSON,recruitIds);
    }
    @RequestMapping("/gr/addRecruitResume.action")
    public Integer addRecruitResume(ServletRequest request,String recruitIds){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        return grService.addRecruitResume(userJSON,recruitIds);
    }
    @RequestMapping(value = "/gr/getRecruitById.action",method={RequestMethod.POST})
    public Recruit getRecruitById(Integer id){
        return grService.getRecruitById(id);
    }

    @RequestMapping(value = "/gr/getCollect.action",method={RequestMethod.POST})
    public List<Collect> getCollect(ServletRequest request){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        return grService.getCollect(userJSON);
    }
    @RequestMapping(value = "/gr/delCollect.action",method={RequestMethod.POST})
    public Integer delCollect(ServletRequest request,Integer id){
        return grService.delCollect(id);
    }
    @RequestMapping(value = "/gr/getJobHuntingLog.action",method={RequestMethod.POST})
    public List<JobHuntingLog> getJobHuntingLog(ServletRequest request){
        HttpServletRequest req=(HttpServletRequest)request;
        String token=req.getHeader("token");
        String userJSON=(String) redisTemplate.opsForValue().get(token);
        return grService.getJobHuntingLog(userJSON);
    }
    @RequestMapping(value = "/gr/delLog.action",method={RequestMethod.POST})
    public Integer delLog(Integer id){
        return grService.delLog(id);
    }

}
